﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Advertise
{
    public class Repost51 : IRepost
    {
        private System.Windows.Forms.WebBrowser webBrowser1 = null;
        private LearnNet.Common.XmlHelper xmlHelper = null;
        private static string code = "51";
        public Repost51(System.Windows.Forms.WebBrowser webBrowser1, LearnNet.Common.XmlHelper xmlHelper)
        {
            this.webBrowser1 = webBrowser1;
            this.xmlHelper = xmlHelper;
        }

        public string  Register(RegistEntity regEntity)
        {
            string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/name", regEntity.Name,webBrowser1,xmlHelper))
            {
                strReturn+="名称设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/pwd", regEntity.Pwd,webBrowser1,xmlHelper))
            {
                strReturn+="密码设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/surepwd", regEntity.SurePwd,webBrowser1,xmlHelper))
            {
                strReturn+="确认密码设置出错!";
            }
           
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/question", regEntity.Question,webBrowser1,xmlHelper))
            {
                strReturn+="问题设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/answer", regEntity.Answer,webBrowser1,xmlHelper))
            {
                strReturn+="答案设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='"+code+"']/register/verificationCode", regEntity.VerificationCode,webBrowser1,xmlHelper))
            {
                strReturn += "验证码设置出错!";
            }
            try
            {
                object obj = webBrowser1.Document.GetElementById("submit").InvokeMember("click");
                //object obj = webBrowser1.Document.Forms["fReg0"].InvokeMember("submit");
            }
            catch (Exception e)
            {
                strReturn += e.Message;
            }
            return strReturn;
        }

        public string LoginIn(System.Windows.Forms.WebBrowser webBrowser1, RegistEntity regEntity)
        {
            string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/login/name", regEntity.Name, webBrowser1, xmlHelper))
            {
                strReturn += "名称设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/login/pwd", regEntity.Pwd, webBrowser1, xmlHelper))
            {
                strReturn += "密码设置出错!";
            }

            try
            {
                object obj = null;
                //HtmlDocument hd = webBrowser1.Document;
                //IHTMLWindow2 win = (IHTMLWindow2)hd.Window.DomWindow;
                //string s = " sinaSSOController.login(" + regEntity.Name + "," + regEntity.Pwd + ",30);";

                //win.execScript(s, "javascript");
                //ReportHelper.CallJsMethod(webBrowser1, "loginByScript", regEntity.Name + "," + regEntity.Pwd+",30");
                obj = webBrowser1.Document.GetElementById("btn_login").InvokeMember("click");
                // webBrowser1.Document.GetElementById("password").SetAttribute("value", regEntity.Pwd);
                 //obj = webBrowser1.Document.Forms[0].InvokeMember("submit");
                // obj = webBrowser1.Document.Forms["loginform"].InvokeMember("submit");
                //HtmlElementCollection htmlCollection = webBrowser1.Document.GetElementsByTagName("input");
                //for (int i = 0; i < htmlCollection.Count; i++)
                //{
                //    HtmlElement element = htmlCollection[i];
                //    string str = element.GetAttribute("name").ToString();
                //    if (str == "passport_51_submit")
                //    {
                //        element.InvokeMember("submit");//
                //        break;
                //    }

                //}

            }
            catch (Exception e)
            {
                strReturn += e.Message;
            }
            return strReturn;
        }

        public string Report(System.Windows.Forms.WebBrowser webBrowser1, ReportContent reportContent)
        {

            string strReturn = string.Empty;
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/report/title", reportContent.Title, webBrowser1, xmlHelper))
            {
                strReturn += "名称设置出错!";
            }
            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/report/content", reportContent.Content, webBrowser1, xmlHelper))
            {
                strReturn += "内容设置出错!";
            }

            if (!ReportHelper.SetAttribute("root/station[@code='" + code + "']/report/classtype", "1", webBrowser1, xmlHelper))
            {
                strReturn += "内容设置出错!";
            }

            try
            {

              
                object obj = null;
                //HtmlElementCollection htmlCollection = webBrowser1.Document.GetElementsByTagName("input");
                //for (int i = 0; i < htmlCollection.Count; i++)
                //{
                //    HtmlElement element = htmlCollection[i];

                //    if (element.Name == "key-093402170-a")
                //    {
                //        element.InvokeMember("click");
                //    }

                //}
                obj = webBrowser1.Document.GetElementById("submit_diary").InvokeMember("click");
                // obj = webBrowser1.Document.Forms["editorForm"].InvokeMember("submit");
                Thread.Sleep(2000);
            }
            catch (Exception e)
            {
                strReturn += e.Message;
            }

            return strReturn;
        }
    }
}
